/* add user code begin Header */
/**
  **************************************************************************
  * @file     at32f423_int.c
  * @brief    main interrupt service routines.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
/* add user code end Header */

/* includes ------------------------------------------------------------------*/
#include "at32f423_int.h"
#include "wk_system.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "tmc.h"
#include "motor_bsp.h"
#include "usb_cdc.h"
#include "usb_serial.h"
#include "at32f423_wk_config.h"
#include "bsp_adc.h"
#include "multi_timer.h"
#include "hmi_serial.h"
#include "perf_counter.h"
/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */

/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */

/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */

/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */

/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */

/* add user code end 0 */

/* external variables ---------------------------------------------------------*/
/* add user code begin external variables */

/* add user code end external variables */

/**
  * @brief  this function handles nmi exception.
  * @param  none
  * @retval none
  */
void NMI_Handler(void)
{
  /* add user code begin NonMaskableInt_IRQ 0 */

  /* add user code end NonMaskableInt_IRQ 0 */

  /* add user code begin NonMaskableInt_IRQ 1 */

  /* add user code end NonMaskableInt_IRQ 1 */
}

/**
  * @brief  this function handles hard fault exception.
  * @param  none
  * @retval none
  */
void HardFault_Handler(void)
{
  /* add user code begin HardFault_IRQ 0 */

  /* add user code end HardFault_IRQ 0 */
  /* go to infinite loop when hard fault exception occurs */
  while (1)
  {
    /* add user code begin W1_HardFault_IRQ 0 */

    /* add user code end W1_HardFault_IRQ 0 */
  }
}

/**
  * @brief  this function handles memory manage exception.
  * @param  none
  * @retval none
  */
void MemManage_Handler(void)
{
  /* add user code begin MemoryManagement_IRQ 0 */

  /* add user code end MemoryManagement_IRQ 0 */
  /* go to infinite loop when memory manage exception occurs */
  while (1)
  {
    /* add user code begin W1_MemoryManagement_IRQ 0 */

    /* add user code end W1_MemoryManagement_IRQ 0 */
  }
}

/**
  * @brief  this function handles bus fault exception.
  * @param  none
  * @retval none
  */
void BusFault_Handler(void)
{
  /* add user code begin BusFault_IRQ 0 */

  /* add user code end BusFault_IRQ 0 */
  /* go to infinite loop when bus fault exception occurs */
  while (1)
  {
    /* add user code begin W1_BusFault_IRQ 0 */

    /* add user code end W1_BusFault_IRQ 0 */
  }
}

/**
  * @brief  this function handles usage fault exception.
  * @param  none
  * @retval none
  */
void UsageFault_Handler(void)
{
  /* add user code begin UsageFault_IRQ 0 */

  /* add user code end UsageFault_IRQ 0 */
  /* go to infinite loop when usage fault exception occurs */
  while (1)
  {
    /* add user code begin W1_UsageFault_IRQ 0 */

    /* add user code end W1_UsageFault_IRQ 0 */
  }
}

/**
  * @brief  this function handles svcall exception.
  * @param  none
  * @retval none
  */
void SVC_Handler(void)
{
  /* add user code begin SVCall_IRQ 0 */

  /* add user code end SVCall_IRQ 0 */
  /* add user code begin SVCall_IRQ 1 */

  /* add user code end SVCall_IRQ 1 */
}

/**
  * @brief  this function handles debug monitor exception.
  * @param  none
  * @retval none
  */
void DebugMon_Handler(void)
{
  /* add user code begin DebugMonitor_IRQ 0 */

  /* add user code end DebugMonitor_IRQ 0 */
  /* add user code begin DebugMonitor_IRQ 1 */

  /* add user code end DebugMonitor_IRQ 1 */
}

/**
  * @brief  this function handles pendsv_handler exception.
  * @param  none
  * @retval none
  */
void PendSV_Handler(void)
{
  /* add user code begin PendSV_IRQ 0 */

  /* add user code end PendSV_IRQ 0 */
  /* add user code begin PendSV_IRQ 1 */

  /* add user code end PendSV_IRQ 1 */
}

/**
  * @brief  this function handles systick handler.
  * @param  none
  * @retval none
  */
void SysTick_Handler(void)
{
  /* add user code begin SysTick_IRQ 0 */
    Motor_task_callback();
    timer_ticks();
    user_code_insert_to_systick_handler();
  /* add user code end SysTick_IRQ 0 */

  wk_timebase_handler();

  /* add user code begin SysTick_IRQ 1 */

  /* add user code end SysTick_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 1 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel1_IRQHandler(void)
{
  /* add user code begin DMA1_Channel1_IRQ 0 */
	dma_flag_clear(DMA1_GL1_FLAG);
	bsp_adc_isr();
  /* add user code end DMA1_Channel1_IRQ 0 */
  /* add user code begin DMA1_Channel1_IRQ 1 */

  /* add user code end DMA1_Channel1_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 2 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel2_IRQHandler(void)
{
  /* add user code begin DMA1_Channel2_IRQ 0 */
	
	if(dma_interrupt_flag_get(DMA1_FDT2_FLAG))
    {
        dma_flag_clear(DMA1_GL2_FLAG);
        tmc_isr();
    }

  /* add user code end DMA1_Channel2_IRQ 0 */
  /* add user code begin DMA1_Channel2_IRQ 1 */

  /* add user code end DMA1_Channel2_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 3 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel3_IRQHandler(void)
{
  /* add user code begin DMA1_Channel3_IRQ 0 */
	if(dma_interrupt_flag_get(DMA1_FDT3_FLAG))
    {
        dma_flag_clear(DMA1_GL3_FLAG);
    }

  /* add user code end DMA1_Channel3_IRQ 0 */
  /* add user code begin DMA1_Channel3_IRQ 1 */

  /* add user code end DMA1_Channel3_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 4 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel4_IRQHandler(void)
{
  /* add user code begin DMA1_Channel4_IRQ 0 */
	if(dma_interrupt_flag_get(DMA1_FDT4_FLAG))
    {
        dma_flag_clear(DMA1_GL4_FLAG);
        motor_isr(AFMotor);
    }

  /* add user code end DMA1_Channel4_IRQ 0 */
  /* add user code begin DMA1_Channel4_IRQ 1 */

  /* add user code end DMA1_Channel4_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 5 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel5_IRQHandler(void)
{
  /* add user code begin DMA1_Channel5_IRQ 0 */
	if(dma_interrupt_flag_get(DMA1_FDT5_FLAG))
    {
        dma_flag_clear(DMA1_GL5_FLAG);
        motor_isr(APMotor);
    }

  /* add user code end DMA1_Channel5_IRQ 0 */
  /* add user code begin DMA1_Channel5_IRQ 1 */

  /* add user code end DMA1_Channel5_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 6 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel6_IRQHandler(void)
{
  /* add user code begin DMA1_Channel6_IRQ 0 */
    if(dma_interrupt_flag_get(DMA1_FDT6_FLAG))
    {
        dma_flag_clear(DMA1_GL6_FLAG);
        hmi_serial_isr_handler(HMI_SERIAL_BUF_RX_SIZE);
    }
  /* add user code end DMA1_Channel6_IRQ 0 */
  /* add user code begin DMA1_Channel6_IRQ 1 */

  /* add user code end DMA1_Channel6_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 7 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel7_IRQHandler(void)
{
  /* add user code begin DMA1_Channel7_IRQ 0 */
	if(dma_interrupt_flag_get(DMA1_GL7_FLAG))
		dma_flag_clear(DMA1_GL7_FLAG);
  /* add user code end DMA1_Channel7_IRQ 0 */
  /* add user code begin DMA1_Channel7_IRQ 1 */

  /* add user code end DMA1_Channel7_IRQ 1 */
}

/**
  * @brief  this function handles EXINT Line [9:5] handler.
  * @param  none
  * @retval none
  */
void EXINT9_5_IRQHandler(void)
{
  /* add user code begin EXINT9_5_IRQ 0 */
	if(exint_flag_get(PI1_PIN))
    {
        exint_flag_clear(PI1_PIN);
        motor_pi_isr(AFMotor);
        usb_serial_update_tick_af();
        Motor_task_pi_calc_af(gpio_input_data_bit_read(PI1_GPIO_PORT, PI1_PIN));
        //printf("af pos %d \n",motor_pos_get(AFMotor));
    }
    
    if(exint_flag_get(PI3_PIN))
    {
        exint_flag_clear(PI3_PIN);
        af_ring_detection(gpio_input_data_bit_read(PI3_GPIO_PORT, PI3_PIN),gpio_input_data_bit_read(PI4_GPIO_PORT, PI4_PIN));
		usb_serial_update_af_ring();
    }
    
    if(exint_flag_get(PI4_PIN))
    {
        exint_flag_clear(PI4_PIN);
        af_ring_detection(gpio_input_data_bit_read(PI3_GPIO_PORT, PI3_PIN),gpio_input_data_bit_read(PI4_GPIO_PORT, PI4_PIN));
		usb_serial_update_af_ring();
    }

  /* add user code end EXINT9_5_IRQ 0 */
  /* add user code begin EXINT9_5_IRQ 1 */

  /* add user code end EXINT9_5_IRQ 1 */
}

/**
  * @brief  this function handles USART1 handler.
  * @param  none
  * @retval none
  */
void USART1_IRQHandler(void)
{
  /* add user code begin USART1_IRQ 0 */

  /* add user code end USART1_IRQ 0 */
  /* add user code begin USART1_IRQ 1 */

  /* add user code end USART1_IRQ 1 */
}

/**
  * @brief  this function handles USART2 handler.
  * @param  none
  * @retval none
  */
void USART2_IRQHandler(void)
{
  /* add user code begin USART2_IRQ 0 */

  /* add user code end USART2_IRQ 0 */
  /* add user code begin USART2_IRQ 1 */

  /* add user code end USART2_IRQ 1 */
}

/**
  * @brief  this function handles EXINT Line [15:10] handler.
  * @param  none
  * @retval none
  */
void EXINT15_10_IRQHandler(void)
{
  /* add user code begin EXINT15_10_IRQ 0 */
    if(exint_flag_get(PI2_PIN))
    {
        exint_flag_clear(PI2_PIN);
        motor_pi_isr(APMotor);
        usb_serial_update_tick_ap();
        Motor_task_pi_calc_ap(gpio_input_data_bit_read(PI2_GPIO_PORT, PI2_PIN));
    }

  /* add user code end EXINT15_10_IRQ 0 */
  /* add user code begin EXINT15_10_IRQ 1 */

  /* add user code end EXINT15_10_IRQ 1 */
}

/**
  * @brief  this function handles OTGFS1 handler.
  * @param  none
  * @retval none
  */
void OTGFS1_IRQHandler(void)
{
  /* add user code begin OTGFS1_IRQ 0 */
	usb_cdc_irq_handler();

  /* add user code end OTGFS1_IRQ 0 */

  /* add user code begin OTGFS1_IRQ 1 */

  /* add user code end OTGFS1_IRQ 1 */
}

/**
  * @brief  this function handles ACC handler.
  * @param  none
  * @retval none
  */
void ACC_IRQHandler(void)
{
  /* add user code begin ACC_IRQ 0 */

  /* add user code end ACC_IRQ 0 */
  /* add user code begin ACC_IRQ 1 */

  /* add user code end ACC_IRQ 1 */
}

/* add user code begin 1 */

/* add user code end 1 */
